package com.jc.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jc.auth.application.convert.AuthPermissionDTOConverter;
import com.jc.auth.application.convert.AuthRolePermissionDTOConverter;
import com.jc.auth.application.dto.AuthPermissionDTO;
import com.jc.auth.application.dto.AuthRolePermissionDTO;
import com.jc.auth.common.entity.Result;
import com.jc.auth.domain.entity.AuthPermissionBO;
import com.jc.auth.domain.entity.AuthRolePermissionBO;
import com.jc.auth.domain.service.AuthRolePermissionDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Description:
 * @Author: ASL_ly
 * @Package: com.jc.auth.application.controller
 * @Project: jc-club
 * @Date: 2024/10/29  18:58
 */
@RestController
@Slf4j
@RequestMapping("/rolePermission")
public class RolePermissionController {
    @Resource
    private AuthRolePermissionDomainService authRolePermissionDomainService;

    /**
     * 新增角色权限
     * @param authRolePermissionDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody AuthRolePermissionDTO authRolePermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("rolePermissionDTOController.add.dto:{}", JSON.toJSONString(authRolePermissionDTO));
            }
            Preconditions.checkNotNull(authRolePermissionDTO.getRoleId(), "角色id不能为空");
            AuthRolePermissionBO authRolePermissionBO = AuthRolePermissionDTOConverter
                    .INSTANCE.convertRolePermissionDTOToBO(authRolePermissionDTO);
            Boolean result = authRolePermissionDomainService.add(authRolePermissionBO);
            return Result.success(result);
        } catch (Exception e) {
            log.error("rolePermissionController.add.dto:{}", e.getMessage(), e);
            return Result.fail("新增角色权限失败");
        }
    }
}
